package com.example.luke.shengdangxiaoattendance.Model;

import android.content.Context;

import com.example.luke.shengdangxiaoattendance.Database.DingHanDB;
import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by luke on 23/05/2017.
 */

public class Course {

    public static   final String DH_TABLE_NAME = "GoodList";

    public static   final String  course_Time="course_Time";//商品编号
    public static   final String  course_name_1="course_name_1";//商品编号
    public static   final String  course_name_2="course_name_2";//商品编号
    public static   final String  course_teacher="course_teacher";//商品编号
    public static   final String  course_class="course_class";//商品编号


    public String Time;
    public String name1;
    public String name2;
    public String teacher;
    public String className;
    private  static  Course course;
    public ArrayList<Course >courseArrayList;

    public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + course_Time + " TEXT," + course_name_1 + " TEXT,"
            + course_name_2 + " TEXT," + course_teacher + " TEXT," + course_class + " TEXT )";
    private Context context;


    public static Course getInstance(Context context){

        if (course==null){
            course=new Course();
            course.context=context;

        }
        return course;

    }

    public static void clearAllCourse(Context context){

        DingHanDB.getInstance(context).clearAllCourse();

    }

    public void storeToDB(Context context, Course course){

        boolean stat=true;


        for (Course temp:courseArrayList){
            if (!temp.Time.equals(course.Time)){
                courseArrayList.add(course);
                stat=false;

            }
        }

        if (stat)
          DingHanDB.getInstance(context).insert(DH_TABLE_NAME,course);



    }

    private ArrayList<Course> getCourseArrayList() {

        return DingHanDB.getInstance(GlobalFunction.context).getAllCourseList();


    }

    public Course getCurrentCourse() throws ParseException {
        courseArrayList=getCourseArrayList();


        Course temp = null;

        int lastCourseIndex;

        for (int i=0;i<courseArrayList.size();i++){

            Course course=courseArrayList.get(i);

            if (i==0){

            long minDiffer=GlobalFunction.getHourMinDifference(course.getStartTime(),new Date());

                if (minDiffer<=90)//早上的课，一个半小时前，提前显示
                {

                    temp=course;

                }

            }


            if (course.Time.contains("-")) {

//                GlobalFunction.printTextByTag(course.Time+":"+course.className);
                String[] timeInterval = course.Time.split("-");

                boolean stat = GlobalFunction.isTimeBetweenTwoTime(timeInterval[0] + ":00", timeInterval[1] + ":00");
                if (stat) {
                    temp = course;


                }
                else {

                    if (new Date().after(course.getEndTime())){

                        int nextLesson=i+1;

                        if (nextLesson<courseArrayList.size()){

                            temp=courseArrayList.get(nextLesson);


                        }
                    }
                }
            }


        }

        return temp;

    }

    public Date getStartTime() {

        String[] timeInterval=  Time.split("-");

       String date= GlobalFunction.convertDate(new Date(),"yyyy-MM-dd");

        return GlobalFunction.convertStrToDate(date+" "+timeInterval[0],"yyyy-MM-dd HH:mm");

    }

    public Date getEndTime() {

        String[] timeInterval=  Time.split("-");

        String date= GlobalFunction.convertDate(new Date(),"yyyy-MM-dd");

        return GlobalFunction.convertStrToDate(date+" "+timeInterval[1],"yyyy-MM-dd HH:mm");

    }
}
